﻿using System.Collections;
using System.Collections.Generic;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using NpcFramework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class AddVehicleFollowPath : LatentActionNodeBase
    {
        public BBParameter<int> carNpcId;
        public BBParameter<int> pathId;
        public BBParameter<string> birthPoint;
        
        public BBParameter<string> pathRes;

        private ResLoader _pathLoader;
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }

        public IEnumerator Invoke()
        {
            if(_pathLoader == null) _pathLoader = ResLoader.Alloc();
            bool pathSuccess = false;
            PathData pathData = new PathData();

            if (!string.IsNullOrEmpty(pathRes.value))
            {
                _pathLoader.Add2Load(pathRes.value, ((success, assetName, asset) =>
                {
                    if (!success)
                    {
                        return;
                    }

                    var pathObject = GameObject.Instantiate((asset as GameObject));
                    var path = pathObject.GetComponentInChildren<NpcWayPath>();
                    if (path == null) return;
                    pathData.id = path.WayID;
                    pathData.loop = path.IsLoop;
                    pathData.pathType = PathType.Car;

                    pathData.pathPoints = new List<PathPointData>(50);

                    for (int j = 0; j < path.WayPoints.Count; j++)
                    {
                        pathData.pathPoints.Add(TrafficSystemHolder.GetPointData(path.WayPoints[j],
                            PathPointType.Normal, PathType.Car));
                    }

                    pathData.prefabPath = "";
                    pathData.position = path.transform.position;

                    GameObject.Destroy(pathObject);

                    if (pathData.pathPoints.Count < 2) return;

                    pathSuccess = true;
                }));

                yield return new UnityEngine.WaitUntil(() => pathSuccess);
            }
            else
            {
                // 已有车辆路径
                pathData = LibertyAIUtils.TrafficManager.PathManager.GetPath(pathId.value);
                if(pathData == null) yield break;
            }

            var p = BlueprintUtils.GetBlueprintPoint(birthPoint.value);
            if (p == null) yield break;

            CarBirthData birthCarData = new CarBirthData();
            birthCarData.position = p.position;
            birthCarData.forward = p.forward;
            birthCarData.random = false;
            birthCarData.carModelId = carNpcId.value;
            birthCarData.pathId = pathData.id;
            birthCarData.ignoreLimit = true;
            LibertyAIUtils.AIManager.CarBirthManager.AddCar(birthCarData);
            CarEntity carEntity = LibertyAIUtils.AIManager.CarBirthManager.GetCarEntity(birthCarData.uniqueId);
            if (carEntity && carEntity.data!=null)
            {
                carEntity.data.carStatus = CarStatus.PathControl;
            }
        }
        
        public override void OnGraphStoped()
        {
            if (_pathLoader != null) 
            {
                _pathLoader.Recycle2Cache();
                _pathLoader = null;
            }
        }
    }
}